package hotel.menu.activity;

import java.util.ArrayList;
import java.util.List;
import com.example.youknow.R;

import hotel.menu.adapter.DianCanPageOneAdapter;
import hotel.menu.adapter.WaiMaiPageOneAdapter;
import hotel.menu.domain.Order;
import hotel.menu.domain.OrderMessage;
import hotel.menu.net.NetUtil;
import hotel.menu.untils.ConstantID;
import hotel.menu.untils.SharedPreferencesUntil;
import hotel.menu.untils.XListView;
import hotel.menu.untils.XListView.IXListViewListener;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DianCanPageOne implements IXListViewListener {
	private DianCan context;
	private View layout;
	private XListView listView;
	private DianCanPageOneAdapter adapter;
	private List<OrderMessage> list = new ArrayList<OrderMessage>();
	private List<OrderMessage> interim;
	private LinearLayout hint;
	private final int GETDATA = 0001;
	private LinearLayout loading;

	public DianCanPageOne(DianCan dianCan, View view) {
		this.context = dianCan;
		this.layout = view;
		findID();
		loading.setVisibility(0);
		hint.setVisibility(8);
		listView.setVisibility(8);
		getData();

	}

	private void getData() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				interim = NetUtil.getSingleNetUtil().WaiMaiWaitOrder(
						SharedPreferencesUntil.GetInt(context, "BUYERID"),
						ConstantID.PAGEONE,ConstantID.WAIT_SUBMIT);

				handler.sendEmptyMessage(GETDATA); 

			}
		}).start();

	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == GETDATA) {
				if (interim != null&&interim.size()>0) {
					list.addAll(interim);
					ConstantID.PAGEONE++;
				}

				if (list.size() > 0) {
					hint.setVisibility(8);
					listView.setVisibility(0);
					if (adapter == null) {
						adapter = new DianCanPageOneAdapter(context, list);
						listView.setAdapter(adapter);
						listView.setPullLoadEnable(true);
						listView.setXListViewListener(DianCanPageOne.this);
					} else {
						adapter.notifyDataSetChanged();

					}

				} else {
					hint.setVisibility(0);
					listView.setVisibility(4);
				}
				loading.setVisibility(8);
			}
		}
	};

	private void findID() {
		listView = (XListView) layout.findViewById(R.id.diancan_page1_listview);
		hint = (LinearLayout) layout.findViewById(R.id.diancan_page1_hint);
		loading = (LinearLayout) layout.findViewById(R.id.diancan_page1_loading);
		hint.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				loading.setVisibility(0);
				hint.setVisibility(8);
				listView.setVisibility(8);
				getData();
			}
		});
	}

	@Override
	public void onLoadMore() {
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				getData();
				adapter.notifyDataSetChanged();
				onLoad();
			}
		}, 2000);
	}

	private void onLoad() {
		listView.stopRefresh();
		listView.stopLoadMore();
	}

}
